{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Работа с распределениями в Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<strong>Автор:</strong> Маркашева Вера Алексеевна"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "В SciPy можно работать с большим количеством распределений. Вот некоторые из них: \n",
    "\n",
    "<table class=\"shadedrows\">\n",
    "<tr>\n",
    "<th><strong>Распределение</strong></th>\n",
    "<th><strong>Название в SciPy</strong></th>\n",
    "<th><strong>Параметры</strong></th>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Биномиальное</td>\n",
    "<td><code>binom</code></td>\n",
    "<td><code>size</code>, <code>prob</code></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Пуассона</td>\n",
    "<td><code>poisson</code></td>\n",
    "<td><code>lambda</code></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Равномерное</td>\n",
    "<td><code>uniform</code></td>\n",
    "<td></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Экспоненциальное</td>\n",
    "<td><code>expon</code></td>\n",
    "<td></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Нормальное</td>\n",
    "<td><code>norm</code></td>\n",
    "<td></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Коши</td>\n",
    "<td><code>cauchy</code></td>\n",
    "<td></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Хи-квадрат</td>\n",
    "<td><code>chi2</code></td>\n",
    "<td><code>df</code></td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Стьюдента</td>\n",
    "<td><code>t</code></td>\n",
    "<td><code>df</code></td>\n",
    "</tr>\n",
    "</table>\n",
    "\n",
    "Другие распределения можно найти тут: https://www.johndcook.com/blog/distributions_scipy/ или https://docs.scipy.org/doc/scipy/reference/stats.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dist1 = stats.uniform(0, 1) # равномерное распределение на [0,1]\n",
    "dist2 = stats.uniform(2, 3) # равномерное распределение на [2,5] (!)\n",
    "dist3 = stats.norm(0, 2) # нормальное распределение N(0,2^2)\n",
    "dist4 = stats.norm() # стандартное нормальное распределение N(0,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "После того, как Вы определите распределение, сможете использовать следующие методы (некоторые из них применимы только к непрерывным распределениям):\n",
    "    \n",
    "<table style=\"text-align:left;\">\n",
    "<tr>\n",
    "<th><strong>Метод</strong></th>\n",
    "<th><strong>Значение</strong></th>\n",
    "</tr>\n",
    "<tr>\n",
    "<td><code>pdf</code></td>\n",
    "<td>плотность распределения</td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td><code>cdf</code></td>\n",
    "<td>функция распределения</td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td><code>ppf</code></td>\n",
    "<td>фукция для квантилей (обратная функция к функции распределения)</td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td><code>stats</code></td>\n",
    "<td>характеристики распределения: среднее, дисперсия и т.д.</td>\n",
    "</tr>\n",
    "</table>\n",
    "\n",
    "Подробнее, опять же, тут: https://www.johndcook.com/blog/distributions_scipy/ или https://docs.scipy.org/doc/scipy/reference/stats.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6448536269514722"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist4.ppf(0.95)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6666666666666666"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist2.cdf(4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3989422804014327"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist4.pdf(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.919927969080108"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist3.ppf(1-0.05/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.975"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dist3.cdf(3.919927969080108)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
